package cn.sangyy.org.controller;

import cn.sangyy.basic.constent.BaseConstants;
import cn.sangyy.basic.utils.AjaxResult;
import cn.sangyy.basic.utils.PageList;
import cn.sangyy.org.dto.ShopAuditDto;
import cn.sangyy.org.dto.ShopRegisterDto;
import cn.sangyy.org.service.IShopService;
import cn.sangyy.org.domain.Shop;
import cn.sangyy.org.query.ShopQuery;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理")
public class ShopController {
    @Autowired
    public IShopService shopService;

    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody @Valid Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.updateShop(shop);
            else
                shopService.insert(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    /**
    * 通过id获取对象信息
    * @param id
    * @return
    */
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id) {
        try {
            Shop shop = shopService.selectById(id);
            return AjaxResult.me().setResultObj(shop);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取一个失败！"+e.getMessage());
        }
    }

    /**
    * 查看所有
    * @return
    */
    @GetMapping("/list")
    public AjaxResult list(){
        try {
            List<Shop> list = shopService.selectList(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setCode(201).setMessage("获取所有失败！"+e.getMessage());
        }
    }

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping()
    public AjaxResult listPageByQuery(@RequestBody ShopQuery query) {
        Page<Shop> page = new Page<Shop>(query.getPage(),query.getRows());
        EntityWrapper<Shop> wrapper = new EntityWrapper<>();
        wrapper.like("name",query.getKeyword()).or().like("address",query.getKeyword());
        page = shopService.selectPage(page,wrapper);
        PageList<Shop> pageList = new PageList<Shop>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }

    /**
    * @author Sangyy
    * 店铺入驻
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody @Valid ShopRegisterDto shopRegisterDto){
        shopService.settlement(shopRegisterDto);
        return AjaxResult.me();
    }

    /**
    * @author Sangyy
    * 店铺激活
    */
    @GetMapping("/active/{id}")
    public String active(@PathVariable Long id){
        String msg = "激活成功！";
        Shop shop = shopService.selectById(id);
        if(shop.getState() == 3){
            msg = "已激活，请勿重复激活";
        }
        shop.setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
        shopService.updateById(shop);
        return msg;
    }

    /**
    * @author Sangyy
    * 人工审核店铺信息
    */
    @PostMapping("/audit")
    public AjaxResult audit(@RequestBody @Valid ShopAuditDto shopAuditDto, HttpServletRequest request){
        shopService.audit(shopAuditDto,request);
        return AjaxResult.me();
    }


}
